package com.ljyhhh.SmartCity

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.ljyhhh.SmartCity.activity.fragment.brokenLine.BrokenLine
import com.ljyhhh.SmartCity.activity.fragment.login.FragmentLogin
import com.ljyhhh.SmartCity.activity.fragment.homePage.HomePage
import com.ljyhhh.SmartCity.activity.fragment.orientationv.BaiduOrientation
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    private lateinit var fragments:Array<Fragment>
    private var lastfragment =0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initFragment()

    }
    private fun initFragment() {
        val homePage = HomePage()
        val baiduOrientation = BaiduOrientation()
        val login = FragmentLogin()
        val brokenLine=BrokenLine()
        fragments = arrayOf(homePage,brokenLine ,baiduOrientation, login)


        supportFragmentManager.beginTransaction().apply {
            for (index in fragments.indices) {
                Log.d("index",index.toString())
                add(R.id.main_view, fragments[index],fragments[index]::class.java.name)
                if (index != lastfragment)
                    hide(fragments[index])
            }
            commitAllowingStateLoss()
        }
//        supportFragmentManager.beginTransaction().replace(R.id.main_view,homePage).show(homePage).commit();


//        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
        bnv.itemIconTintList = null
        bnv.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.id1 -> {
                    showAndHideFragment(fragments[0], fragments[lastfragment])
                    lastfragment = 0
                    true
                }
                R.id.id2 -> {
                    showAndHideFragment(fragments[1], fragments[lastfragment])
                    lastfragment = 1
                    true
                }
                R.id.id3 -> {
                    showAndHideFragment(fragments[2], fragments[lastfragment])
                    lastfragment = 2
                    true
                }
                R.id.id4 -> {
                    showAndHideFragment(fragments[3], fragments[lastfragment])
                    lastfragment = 3
                    true
                }


                else -> {false}
            }
        }
    }
    private fun showAndHideFragment(show: Fragment, hide: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        if (show !== hide) transaction.show(show).hide(hide).commitAllowingStateLoss()
    }


}